package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.OrderMapper;
import com.kgc.entity.Order;
import com.kgc.entity.OrderDetail;
import com.kgc.entity.Product;
import com.kgc.service.OrderService;
import com.kgc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author :qinya
 * @PackageName:com.kgc.service.impl
 * @ClassName: OrderServiceImpl
 * @Description: TODO
 * @date : 2025/5/23,11:42
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductService productService;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Order> selectByPage(Map<String, Object> params) {
        PageHelper.startPage((int) params.get("pageNum"), (int) params.get("pageSize"), "createTime desc");
        List<Order> list = orderMapper.selectOrderByPage(params);
        return new PageInfo<>(list);
    }

    @Override
    public boolean orderPay(Map<String, Object> params) {
        return orderMapper.updateOrderPay(params) == 1;
    }

    @Override
    public boolean orderRefund(String serialNumber) {
        boolean temp = orderMapper.updateOrderRefund(serialNumber) == 1;
        //退款成功
        if (temp) {
            Order order = selectBySerialNumber(serialNumber);
            changeStock(order.getId(), "+");
        }
        return temp;
    }

    @Override
    public boolean closeOrder(String serialNumber) {
        boolean temp = orderMapper.updateOrderClose(serialNumber) == 1;
        //订单关闭成功
        if (temp) {
            Order order = selectBySerialNumber(serialNumber);
            changeStock(order.getId(), "+");
        }
        return temp;
    }

    @Override
    public boolean delOrder(int id) {
        return orderMapper.deleteOrder(id) == 1;
    }

    @Override
    public boolean changeStock(int orderId, String flag) {
        List<OrderDetail> list = orderMapper.selectProductQuantity(orderId);
        boolean temp = true;
        for (OrderDetail orderDetail : list) {
            Product product = productService.selectById(String.valueOf(orderDetail.getProductId()));
            //订单关闭或订单退款，收回商品预出库
            if ("+".equals(flag)) {
                product.setStock(product.getStock() + orderDetail.getQuantity());
                product.setPreOut(product.getPreOut() - orderDetail.getQuantity());
            }
            //订单生成，作预出库
            else if ("-".equals(flag)) {
                product.setStock(product.getStock() - orderDetail.getQuantity());
                product.setPreOut(product.getPreOut() + orderDetail.getQuantity());
            }
            temp = productService.editProductStock(product);
        }
        return temp;
    }

    @Override
    public Order selectBySerialNumber(String serialNumber) {
        return orderMapper.selectOrderBySerialNumber(serialNumber);
    }

    @Override
    public boolean addOrder(Order order) {
        return orderMapper.insertOrder(order) == 1;
    }

    @Override
    public boolean addOrderDetail(OrderDetail orderDetail) {
        return orderMapper.insertOrderDetail(orderDetail) == 1;
    }
}
